package com.fr.chart.plot;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.DateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.chart.ChartModule;
import com.fr.chart.LineStyleInfo;
import com.fr.chart.axis.DataLabelInfo;
import com.fr.chart.axis.SeriesAttrAlpha;
import com.fr.chart.axis.SeriesAttrBackground;
import com.fr.chart.axis.SeriesAttrColor;
import com.fr.chart.axis.SeriesAttrLine;
import com.fr.chart.axis.SeriesAttrMarkerType;
import com.fr.chart.axis.SeriesAttrTrendLine;
import com.fr.chart.base.equals.Equals;
import com.fr.chart.core.ChartUtils;
import com.fr.chart.core.ChartXMLCompatibleUtils;
import com.fr.chart.core.TrendLine;
import com.fr.chart.core.glyph.LegendGlyph;
import com.fr.chart.core.glyph.PlotGlyph;
import com.fr.chart.core.glyph.TextGlyph;
import com.fr.chart.legend.Legend;
import com.fr.chart.legend.LegendItem;
import com.fr.chart.legend.LineMarkerIcon;
import com.fr.chart.marker.Marker;
import com.fr.chart.marker.MarkerFactory;
import com.fr.data.ChartData;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.util.Utils;
import java.util.Date;

public abstract class Plot extends ChartModule
{
  private static final long serialVersionUID = 6362269816037714449L;
  public static final String XML_TAG = "Plot";
  private SeriesCollection seriesCollection = new SeriesCollection();
  private NameJavaScriptGroup hotHyperLink;
  private String hotTooltip = null;
  private boolean isNullValueBreak = true;
  protected Legend legend = new Legend();

  public void setLegend(Legend paramLegend)
  {
    this.legend = paramLegend;
  }

  public Legend getLegend()
  {
    return this.legend;
  }

  public void setSeriesCollection(SeriesCollection paramSeriesCollection)
  {
    this.seriesCollection = paramSeriesCollection;
  }

  public SeriesCollection getSeriesCollection()
  {
    return this.seriesCollection;
  }

  public void setHotHyperLink(NameJavaScriptGroup paramNameJavaScriptGroup)
  {
    this.hotHyperLink = paramNameJavaScriptGroup;
  }

  public NameJavaScriptGroup getHotHyperLink()
  {
    return this.hotHyperLink;
  }

  public void setHotTooltip(String paramString)
  {
    this.hotTooltip = paramString;
  }

  public String getHotTooltip()
  {
    return this.hotTooltip;
  }

  public void setNullValueBreak(boolean paramBoolean)
  {
    this.isNullValueBreak = paramBoolean;
  }

  public boolean isNullValueBreak()
  {
    return this.isNullValueBreak;
  }

  public abstract PlotGlyph createPlotGlyph(ChartData paramChartData);

  public void install4PlotGlyph(PlotGlyph paramPlotGlyph, ChartData paramChartData)
  {
    addSeries2PlotGlyph(paramPlotGlyph, paramChartData);
    paramPlotGlyph.setInfo(getInfo());
    paramPlotGlyph.setSeriesCollection(getSeriesCollection());
    paramPlotGlyph.setHotHyperLink(this.hotHyperLink);
    paramPlotGlyph.setHotTooltip(this.hotTooltip);
    paramPlotGlyph.setIsNullValueBreak(this.isNullValueBreak);
  }

  public LegendGlyph createLegendGlyph(PlotGlyph paramPlotGlyph)
  {
    LegendGlyph localLegendGlyph = null;
    if (this.legend != null)
      localLegendGlyph = this.legend.createLegendGlyph(createLegendItems(paramPlotGlyph));
    return localLegendGlyph;
  }

  public LegendItem[] createLegendItems(PlotGlyph paramPlotGlyph)
  {
    int i = paramPlotGlyph.getSeriesSize();
    for (int j = 0; j < paramPlotGlyph.getSeriesSize(); ++j)
    {
      SeriesAttrTrendLine localSeriesAttrTrendLine1 = (SeriesAttrTrendLine)this.seriesCollection.getSeriesCondition(new SeriesAttrTrendLine(), j);
      if ((localSeriesAttrTrendLine1 != null) && (localSeriesAttrTrendLine1.getLine() != null))
        ++i;
    }
    LegendItem[] arrayOfLegendItem = new LegendItem[i];
    for (int k = 0; k < paramPlotGlyph.getSeriesSize(); ++k)
    {
      arrayOfLegendItem[k] = new LegendItem(paramPlotGlyph.getSeries(k).getSeriesName());
      arrayOfLegendItem[k].setLineMarkerIcon(getLineMarkerIconByIndex(k));
    }
    i = paramPlotGlyph.getSeriesSize();
    for (k = 0; k < paramPlotGlyph.getSeriesSize(); ++k)
    {
      SeriesAttrTrendLine localSeriesAttrTrendLine2 = (SeriesAttrTrendLine)this.seriesCollection.getSeriesCondition(new SeriesAttrTrendLine(), k);
      if ((localSeriesAttrTrendLine2 != null) && (localSeriesAttrTrendLine2.getLine() != null))
      {
        arrayOfLegendItem[i] = new LegendItem(localSeriesAttrTrendLine2.getName());
        LineStyleInfo localLineStyleInfo = localSeriesAttrTrendLine2.getLine().getLineStyleInfo();
        arrayOfLegendItem[i].setLineMarkerIcon(new LineMarkerIcon(localLineStyleInfo.getSeriesLineColor(), 1, null));
        ++i;
      }
    }
    return arrayOfLegendItem;
  }

  protected LineMarkerIcon getLineMarkerIconByIndex(int paramInt)
  {
    Object localObject;
    LineMarkerIcon localLineMarkerIcon = new LineMarkerIcon();
    if (showLegendLine())
    {
      localLineMarkerIcon.setLineStyle(1);
      localObject = (SeriesAttrColor)getSeriesCollection().getSeriesCondition(new SeriesAttrColor(), paramInt);
      localLineMarkerIcon.setColor(((SeriesAttrColor)localObject).getSeriesColor());
    }
    if (showLegendMarker())
    {
      localObject = (SeriesAttrMarkerType)getSeriesCollection().getSeriesCondition(new SeriesAttrMarkerType(), paramInt);
      Marker localMarker = MarkerFactory.createIndexMarker(((SeriesAttrMarkerType)localObject).getMarkerTypeIndex());
      localMarker.setSize(localMarker.getSize() * 0.6D);
      localLineMarkerIcon.setMarker(localMarker);
      SeriesAttrColor localSeriesAttrColor = (SeriesAttrColor)getSeriesCollection().getSeriesCondition(new SeriesAttrColor(), paramInt);
      localMarker.setColor(localSeriesAttrColor.getSeriesColor());
    }
    dealCondition(localLineMarkerIcon, paramInt);
    return ((LineMarkerIcon)localLineMarkerIcon);
  }

  protected void dealCondition(LineMarkerIcon paramLineMarkerIcon, int paramInt)
  {
    SeriesAttrBackground localSeriesAttrBackground = (SeriesAttrBackground)getSeriesCollection().getSeriesCondition(new SeriesAttrBackground(), paramInt);
    paramLineMarkerIcon.setBackground(localSeriesAttrBackground.getSeriesBackground());
    SeriesAttrAlpha localSeriesAttrAlpha = (SeriesAttrAlpha)getSeriesCollection().getSeriesCondition(new SeriesAttrAlpha(), paramInt);
    paramLineMarkerIcon.setAlpha(localSeriesAttrAlpha.getAlpha());
  }

  protected void dealColorCondition(LineMarkerIcon paramLineMarkerIcon, int paramInt)
  {
    SeriesAttrColor localSeriesAttrColor = (SeriesAttrColor)getSeriesCollection().getSeriesCondition(new SeriesAttrColor(), paramInt);
    paramLineMarkerIcon.setColor(localSeriesAttrColor.getSeriesColor());
    SeriesAttrAlpha localSeriesAttrAlpha = (SeriesAttrAlpha)getSeriesCollection().getSeriesCondition(new SeriesAttrAlpha(), paramInt);
    paramLineMarkerIcon.setAlpha(localSeriesAttrAlpha.getAlpha());
  }

  protected LineMarkerIcon getLineMarkerIconByIndex4Line(int paramInt)
  {
    LineMarkerIcon localLineMarkerIcon = new LineMarkerIcon();
    SeriesAttrLine localSeriesAttrLine = (SeriesAttrLine)getSeriesCollection().getSeriesCondition(new SeriesAttrLine(), paramInt);
    if (showLegendLine())
      localLineMarkerIcon.setLineStyle(1);
    if (showLegendMarker())
    {
      SeriesAttrMarkerType localSeriesAttrMarkerType = (SeriesAttrMarkerType)getSeriesCollection().getSeriesCondition(new SeriesAttrMarkerType(), paramInt);
      Marker localMarker = MarkerFactory.createIndexMarker(localSeriesAttrMarkerType.getMarkerTypeIndex());
      localMarker.setSize(localMarker.getSize() * 0.6D);
      localLineMarkerIcon.setMarker(localMarker);
      localMarker.setColor(localSeriesAttrLine.getSeriesLineColor());
    }
    dealLineCondition(localLineMarkerIcon, paramInt);
    return localLineMarkerIcon;
  }

  protected void dealLineCondition(LineMarkerIcon paramLineMarkerIcon, int paramInt)
  {
    SeriesAttrLine localSeriesAttrLine = (SeriesAttrLine)getSeriesCollection().getSeriesCondition(new SeriesAttrLine(), paramInt);
    paramLineMarkerIcon.setColor(localSeriesAttrLine.getSeriesLineColor());
    SeriesAttrAlpha localSeriesAttrAlpha = (SeriesAttrAlpha)getSeriesCollection().getSeriesCondition(new SeriesAttrAlpha(), paramInt);
    paramLineMarkerIcon.setAlpha(localSeriesAttrAlpha.getAlpha());
  }

  protected void addSeries2PlotGlyph(PlotGlyph paramPlotGlyph, ChartData paramChartData)
  {
    addSeriesByIndex(0, paramChartData.getSeriesCount(), paramPlotGlyph, paramChartData);
  }

  protected void addSeriesByIndex(int paramInt1, int paramInt2, PlotGlyph paramPlotGlyph, ChartData paramChartData)
  {
    int i = paramInt1;
    int j = (paramChartData == null) ? 0 : paramChartData.getSeriesCount();
    while ((i < paramInt2) && (i < j))
    {
      DataSeries localDataSeries = new DataSeries(i - paramInt1);
      localDataSeries.setSeriesName(Utils.objectToString(paramChartData.getSeriesLabel(i)));
      paramPlotGlyph.addSeries(localDataSeries);
      for (int k = 0; k < paramChartData.getCategoryLabelCount(); ++k)
      {
        DataPoint localDataPoint = new DataPoint();
        localDataPoint.setIndex(k);
        double d = 0.0D;
        if ((i < paramChartData.getSeriesCount()) && (k < paramChartData.getCategoryLabelCount()))
        {
          Number localNumber = paramChartData.getValueAt(i, k);
          if (localNumber != null)
          {
            d = localNumber.doubleValue();
          }
          else
          {
            localDataPoint.setValueIsNull(true);
            d = 0.0D;
          }
          localDataPoint.setValue(d);
        }
        if (k < paramChartData.getCategoryLabelCount())
          localDataPoint.setCategoryName(Utils.objectToString(paramChartData.getCategoryLabel(k)));
        if (i < paramChartData.getSeriesCount())
          localDataPoint.setSeriesName(Utils.objectToString(paramChartData.getSeriesLabel(i)));
        localDataSeries.addDataPoint(localDataPoint);
        localDataPoint.setPercentValue(getPercent(paramChartData, k, i - paramInt1));
      }
      ++i;
    }
    createDataPointLabel(paramPlotGlyph);
  }

  private double getPercent(ChartData paramChartData, int paramInt1, int paramInt2)
  {
    double d1 = 0.0D;
    double d2 = 0.0D;
    for (int i = 0; i < paramChartData.getSeriesCount(); ++i)
    {
      Number localNumber2 = paramChartData.getValueAt(i, paramInt1);
      if (localNumber2 != null)
        d1 += localNumber2.doubleValue();
    }
    Number localNumber1 = paramChartData.getValueAt(paramInt2, paramInt1);
    if (localNumber1 != null)
      d2 = localNumber1.doubleValue();
    if (d1 == 0.0D)
      return 0.0D;
    return (d2 / d1);
  }

  protected void createDataPointLabel(PlotGlyph paramPlotGlyph)
  {
    int i = 0;
    int j = paramPlotGlyph.getSeriesSize();
    while (i < j)
    {
      DataSeries localDataSeries = paramPlotGlyph.getSeries(i);
      for (int k = 0; k < localDataSeries.getDataPointCount(); ++k)
      {
        DataPoint localDataPoint = localDataSeries.getDataPoint(k);
        if (localDataPoint.isValueIsNull())
          break label59:
        createDataLabel(localDataPoint, i);
      }
      label59: ++i;
    }
  }

  protected void createDataLabel(DataPoint paramDataPoint, int paramInt)
  {
    DataLabelInfo localDataLabelInfo = new DataLabelInfo();
    localDataLabelInfo.dealCondition(getSeriesCollection(), paramInt);
    TextGlyph localTextGlyph = new TextGlyph();
    localTextGlyph.dealCondition(getSeriesCollection(), paramInt);
    String str = localDataLabelInfo.createLabelText(paramDataPoint);
    if ((str != null) && (str.length() > 0))
      localTextGlyph.setText(localDataLabelInfo.createLabelText(paramDataPoint));
    localTextGlyph.setTextAttr(localDataLabelInfo.getTextAttr());
    paramDataPoint.setDataLabel(localTextGlyph);
  }

  protected boolean showLegendLine()
  {
    return false;
  }

  protected boolean showLegendMarker()
  {
    return false;
  }

  public double getMaxValueFromData(ChartData paramChartData)
  {
    double d = 0.0D;
    for (int i = 0; i < paramChartData.getCategoryLabelCount(); ++i)
      for (int j = 0; j < paramChartData.getSeriesCount(); ++j)
      {
        Number localNumber = paramChartData.getValueAt(j, i);
        if (localNumber != null)
        {
          if ((i == 0) && (j == 0))
            break label80:
          d = (localNumber.doubleValue() > d) ? localNumber.doubleValue() : d;
        }
      }
    if (d == getMinValueFromData(paramChartData))
      label80: return (d + 10.0D);
    return d;
  }

  public double getMinValueFromData(ChartData paramChartData)
  {
    double d = 0.0D;
    for (int i = 0; i < paramChartData.getCategoryLabelCount(); ++i)
      for (int j = 0; j < paramChartData.getSeriesCount(); ++j)
      {
        Number localNumber = paramChartData.getValueAt(j, i);
        if (localNumber != null)
        {
          if ((i == 0) && (j == 0))
            break label80:
          d = (localNumber.doubleValue() < d) ? localNumber.doubleValue() : d;
        }
      }
    label80: return d;
  }

  public boolean checkDate(ChartData paramChartData)
  {
    int i = 0;
    int j = paramChartData.getCategoryLabelCount();
    while (i < j)
    {
      if (paramChartData.getCategoryLabel(i) != null)
      {
        Date localDate = DateUtils.object2Date(paramChartData.getCategoryLabel(i), true);
        if (localDate == null)
          return false;
      }
      ++i;
    }
    return true;
  }

  public double[] getDateRange(ChartData paramChartData)
  {
    double d1 = 1.7976931348623157E+308D;
    double d2 = 4.9E-324D;
    int i = 0;
    int j = paramChartData.getCategoryLabelCount();
    while (i < j)
    {
      if (paramChartData.getCategoryLabel(i) != null)
      {
        Date localDate = DateUtils.object2Date(paramChartData.getCategoryLabel(i), true);
        if (localDate != null)
        {
          double d3 = ChartUtils.date2Int(localDate);
          if (d3 < d1)
            d1 = d3;
          if (d3 > d2)
            d2 = d3;
        }
      }
      ++i;
    }
    return new double[] { d1 - 1.0D, d2 + 1.0D };
  }

  public boolean match4GUI(Plot paramPlot)
  {
    if (paramPlot == null)
      return false;
    return ComparatorUtils.equals(getClass().getName(), paramPlot.getClass().getName());
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      Object localObject;
      String str = paramXMLableReader.getTagName();
      if (str.equals("SeriesCollection"))
      {
        localObject = new SeriesCollection();
        if (this instanceof CustomPlot)
          ((SeriesCollection)localObject).setDefaultSeriesAttr(new CustomAttr());
        this.seriesCollection = ChartXMLCompatibleUtils.read65DataSeriesCollection(paramXMLableReader, (SeriesCollection)localObject);
      }
      else if ("NewSeriesCollection".equals(str))
      {
        this.seriesCollection = ((SeriesCollection)paramXMLableReader.readXMLObject(new SeriesCollection()));
      }
      else if ("Legend".equals(str))
      {
        this.legend = ((Legend)paramXMLableReader.readXMLObject(new Legend()));
      }
      else if (("NameJavaScriptGroup".equals(str)) || ("NameHyperlinks".equals(str)) || ("HyperlinkMap".equals(str)))
      {
        this.hotHyperLink = ((NameJavaScriptGroup)paramXMLableReader.readXMLObject(new NameJavaScriptGroup()));
      }
      else if (str.equals("Attr"))
      {
        if ((localObject = paramXMLableReader.getAttr("hotTooltip")) != null)
          this.hotTooltip = ((String)localObject);
        if ((localObject = paramXMLableReader.getAttr("isNullValueBreak")) != null)
          this.isNullValueBreak = Boolean.valueOf((String)localObject).booleanValue();
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    paramXMLPrintWriter.startTAG("Attr").attr("hotTooltip", this.hotTooltip).attr("isNullValueBreak", this.isNullValueBreak).end();
    if (this.seriesCollection != null)
      this.seriesCollection.writeXML(paramXMLPrintWriter);
    if (this.legend != null)
      this.legend.writeXML(paramXMLPrintWriter);
    if (this.hotHyperLink != null)
      this.hotHyperLink.writeXML(paramXMLPrintWriter);
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    Plot localPlot = (Plot)super.clone();
    if (getSeriesCollection() != null)
      localPlot.setSeriesCollection((SeriesCollection)getSeriesCollection().clone());
    if (this.legend != null)
      localPlot.legend = ((Legend)this.legend.clone());
    if (this.hotHyperLink != null)
      localPlot.hotHyperLink = ((NameJavaScriptGroup)this.hotHyperLink.clone());
    return localPlot;
  }

  public boolean equals(Object paramObject)
  {
    if (!(paramObject instanceof Plot))
      return false;
    Plot localPlot = (Plot)paramObject;
    if (!(super.equals(localPlot)))
      return false;
    if (!(Equals.equals(localPlot.seriesCollection, this.seriesCollection)))
      return false;
    if (!(Equals.equals(localPlot.hotHyperLink, this.hotHyperLink)))
      return false;
    if (!(Equals.equals(this.hotTooltip, localPlot.hotTooltip)))
      return false;
    if (!(Equals.equals(localPlot.getLegend(), getLegend())))
      return false;
    return (this.isNullValueBreak == localPlot.isNullValueBreak);
  }
}